﻿Imports System.IO
Imports System.Environment

Public Class ScavengerBrowser
    'Scavenger, a visual basic web browser.
    'Copyright (C) 2011  Tetra Productions Ltd.

    'This program is free software: you can redistribute it and/or modify
    'it under the terms of the GNU General Public License as published by
    'the Free Software Foundation, either version 3 of the License, or
    '(at your option) any later version.

    'This program is distributed in the hope that it will be useful,
    'but WITHOUT ANY WARRANTY; without even the implied warranty of
    'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    'GNU General Public License for more details.

    'You should have received a copy of the GNU General Public License
    'along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Inherits WebBrowser
    Dim HistoryModule As New HistoryModule
    Dim last_URL As String
    Dim sameurl As Boolean
    Private Sub Document_Compelted() Handles Me.DocumentCompleted
        If Me.CanGoBack Then
            Scavenger.BCK.Enabled = True
        Else
            Scavenger.BCK.Enabled = False
        End If
        If Me.CanGoForward Then
            Scavenger.FWD.Enabled = True
        Else
            Scavenger.FWD.Enabled = False
        End If
        'Browsing Textbox and Tool Tip Control
        Dim Title As TabPage = Me.Tag
        If Me.DocumentTitle.Length > 30 Then
            Title.Text = Me.DocumentTitle.Substring(0, 27) & "..."
        Else
            Title.Text = Me.DocumentTitle
            Title.ToolTipText = Me.Url.ToString()
        End If
        If Title.Text = "" Then
            Title.Text = "New Tab"
        End If
        Me.ScriptErrorsSuppressed = True
        Scavenger.URL.Text = Me.Url.ToString()
        Scavenger.Text = Me.DocumentTitle
        URL_Retrieve(Me.Url.ToString())
        HistoryModule.AutoComplete_Read()
        HistoryModule.Generic_Read()
    End Sub
    Private Sub Document_Progress(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles Me.ProgressChanged
        Dim control As ScavengerBrowser = Scavenger.TabControl1.SelectedTab.Tag
        If control.Url = Me.Url Then
            If e.MaximumProgress <> 0 And e.MaximumProgress >= e.CurrentProgress Then
                Scavenger.ToolStripProgressBar1.Visible = True
                Scavenger.ToolStripProgressBar1.Value = Convert.ToInt32(100 * e.CurrentProgress / e.MaximumProgress)
            Else
                With Scavenger.ToolStripProgressBar1
                    .Value = 100
                    .Visible = False
                End With
            End If
        Else
            With Scavenger.ToolStripProgressBar1
                .Value = 100
                .Visible = False
            End With
        End If
    End Sub
    Private Sub File_Download(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FileDownload
        'Dim temp As New Download
        'temp.Show()
    End Sub
    'History Write Module
    Public Sub URL_Retrieve(ByRef CurURL As String)
        URL_Parser(CurURL, last_URL)
    End Sub
    Public Sub URL_Parser(ByRef URL As String, ByRef LastURL As String)
        If URL <> LastURL Then
            sameurl = False
            last_URL = URL
            HistoryModule.Write(URL, sameurl)
        Else
            sameurl = True
        End If
    End Sub
    Public Function URL_Return()
        If last_URL IsNot Nothing Then
            Return last_URL
        Else
            Return "N/A"
        End If
    End Function
    Private Sub Got_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        Scavenger.AcceptButton = Nothing
        Me.WebBrowserShortcutsEnabled = False
        Me.IsWebBrowserContextMenuEnabled = False
    End Sub
End Class
